package com.huibei.wateradmin.authority.util;


import java.sql.Time;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;


/**
 * 时间工具类<br>
 */
public class DateUtil
{

	/**
	 * 功能描述: 获取当前时间<br>
	 */
	public static String getTodayByFormat(String format) {
		java.util.Calendar c = java.util.Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		String szSBDate = sdf.format(c.getTime());
		return szSBDate;
	}

	/**
	 * 取得系统时间
	 */
	public static String getSystemTime() {
		String strTime = "";
		try {
			DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			strTime = df.format(new Date());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return strTime;
	}

	/**
	 * 取得系统时间
	 *
	 * @return
	 */
	public static String getShortSystemTime() {
		String strTime = "";
		try {
			DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
			strTime = df.format(new Date());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return strTime;
	}

	/**
	 * 取得系统日期
	 *
	 * @return
	 */
	public static String getSystemDate() {
		String strDate = "";
		try {
			DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
			strDate = df.format(new Date());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return strDate;
	}

	/**
	 * 取得系统时间
	 *
	 * @return
	 */
	public static String getShortSystemDate() {
		String strTime = "";
		try {
			DateFormat df = new SimpleDateFormat("yyyyMMdd");
			strTime = df.format(new Date());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return strTime;
	}

	/**
	 * 日期转字符串
	 *
	 * @param date
	 * @return
	 */
	public static String dateToString(Date date, String formatStr) {
		String strTime = "";
		if (date != null) {
			DateFormat df = new SimpleDateFormat(formatStr);
			strTime = df.format(date);
		} else {
			strTime = "";
		}
		return strTime;
	}

	/**
	 * 字符串转换到时间格式
	 *
	 * @param dateStr
	 *            需要转换的字符串
	 * @param formatStr
	 *            需要格式的目标字符串 举例 yyyy-MM-dd
	 * @return Date 返回转换后的时间
	 * @throws ParseException
	 *             转换异常
	 */
	public static Date stringToDate(String dateStr, String formatStr) {
		DateFormat sdf = new SimpleDateFormat(formatStr);
		Date date = null;
		try {
			date = sdf.parse(dateStr);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}

	/**
	 * 系统时间加减
	 *
	 * @param date
	 * @param dayNum
	 * @return
	 */
	public static String getOpeDate(String date, int dayNum) {
		Date dt = null;
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		try {
			dt = df.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		GregorianCalendar gc = new GregorianCalendar();
		gc.setTime(dt);
		gc.add(5, dayNum);
		// gc.set(gc.YEAR,gc.get(gc.MONTH),gc.get(gc.DATE));
		return String.valueOf(df.format(gc.getTime()));
	}

	/**
	 * 月份相减
	 *
	 * @param date
	 * @param monthNum
	 * @return
	 */
	public static String getOpeMonth(String date, int monthNum) {
		Date dt = null;
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		try {
			dt = df.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		GregorianCalendar gc = new GregorianCalendar();
		gc.setTime(dt);
		gc.add(2, monthNum);
		return String.valueOf(df.format(gc.getTime()));
	}

	/**
	 * 取得两个日期的时间差
	 */
	public static long getDateDifferent(String date1, String date2) {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		ParsePosition pos = new ParsePosition(0);
		ParsePosition pos1 = new ParsePosition(0);
		Date dt1 = formatter.parse(date1, pos);
		Date dt2 = formatter.parse(date2, pos1);
		long l = (dt2.getTime() - dt1.getTime()) / (3600 * 24 * 1000);
		return l;
	}

	/**
	 *
	 * 功能描述: 时间加减
	 */
	public static Date getOperaTime(Date date, int sec) {
		GregorianCalendar gc = new GregorianCalendar();
		gc.setTime(date);
		gc.add(13, sec);
		return gc.getTime();
	}

	/**
	 *
	 * 功能描述: 时间加减
	 */
	public static Date getOperaTimeByMin(Date date, int minute) {
		GregorianCalendar gc = new GregorianCalendar();
		gc.setTime(date);
		gc.add(12, minute);
		return gc.getTime();
	}

	/**
	 * 格式化Date类型时间
	 * @param dateTime
	 * @return
	 */
	public static Date formatDateTime(Date dateTime) {
		if(null != dateTime) {
			String time = dateToString(dateTime, "yyyy-MM-dd");
			return stringToDate(time, "yyyy-MM-dd");
		}
		return null;
	}


	/**
	 * 字符串 转 时间
	 * @param str
	 * @return
	 */
	public static Time StringToTime(String str){
		Time time= null;
		if(str != null){
			SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
			try {
				time = new Time(sdf.parse(str).getTime());
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		return time;
	}

	/**
	 * 取得系统年月
	 * @return
	 */
	public static String getSystemDateMonth() {
		String strDate = "";
		try {
			DateFormat df = new SimpleDateFormat("yyyy-MM");
			strDate = df.format(new Date());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return strDate;
	}

}
